//
//  Net.m
//  shou6
//
//  Created by FrankChen on 13-1-22.
//  Copyright (c) 2013年 FrankChen. All rights reserved.
//

#import "Net.h"

@implementation Net

@synthesize linkArray;
@synthesize titleArray;

-(NSString *) getHtml:(NSString *)webUrl{
    NSString *retStr =[[NSString alloc]init];
    
    NSURL *url = [NSURL URLWithString:webUrl];
    
    NSData *data = [NSData dataWithContentsOfURL:url];
        
    retStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    if([retStr isEqual:@""]){
        [self performSelectorOnMainThread:@selector(showAlert) withObject:nil waitUntilDone:NO];
    }

    return retStr;
}

-(void)showAlert{
    UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
    [alert show];
}

-(NSMutableArray *) getData:(NSString *)html{
       
    NSString *htmlNew=[html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    NSString *tag1=@"<td bgcolor=\"#FFFFFF\" align=\"left\" height=\"30\">";
    NSString *tag2=@"</tr>";

    NSMutableArray *mutableArray=[[NSMutableArray alloc] init];

    for(int index=0;index<20;index++){

        NSRange range1=[htmlNew rangeOfString:tag1];
        int startIndex1=range1.length+range1.location;
        htmlNew=[htmlNew substringFromIndex:startIndex1];
    
        NSRange range2=[htmlNew rangeOfString:tag2];
    
        int endIndex1=range2.location;
    
        NSString * newInfo=[htmlNew substringToIndex:endIndex1];
        [mutableArray addObject:newInfo];

        htmlNew=[htmlNew substringFromIndex:range2.location];
    
    }

    return mutableArray;
}


-(void)handleData:(NSMutableArray *)dataArray :(int)flag{
    NSMutableArray *_linkArray=[[NSMutableArray alloc]init];
    NSMutableArray *_titleArray=[[NSMutableArray alloc]init];
    for(NSString *str in dataArray){
        NSString *tag4=@"<a href=\"";
        NSRange tag4range=[str rangeOfString:tag4];
        str=[str substringFromIndex:(tag4range.location+tag4.length)];
        
        NSString *tag5=@"\">";

        NSString *tempLink1=[str substringToIndex:([str rangeOfString:tag5].location)];
        
        NSString *temp=[str substringFromIndex:([str rangeOfString:tag5].location+2)];
        
        NSString *weburl=@"http://www.shyp.gov.cn";
        
        /*
        switch (flag) {
            case 1:
                weburl=@"http://www.shyp.gov.cn/xwzx/ypyw";
                break;
            case 2:
                weburl=@"http://www.shyp.gov.cn/xwzx/jzdt";
                break;
            case 3:
                weburl=@"http://www.shyp.gov.cn/xwzx/bmdt";
                break;
        }
        */
        
        NSMutableString *tempLink=[[NSMutableString alloc]initWithString:weburl];
        [tempLink insertString:tempLink1 atIndex:[tempLink length]];
        [_linkArray addObject:tempLink];
        
        //获取正文标题
        NSString *tag2=@"</a>";
        int tag2Location=[temp rangeOfString:tag2].location;
        NSString *tempDataContenet=[temp substringToIndex:tag2Location];

        //正文时间
        temp=[temp substringFromIndex:(tag2Location+4)];
        NSString *tag3=@"2";
        int tag3Location=[temp rangeOfString:tag3].location-1;
        temp=[temp substringFromIndex:tag3Location];
        NSString *tempDataTime=[temp substringToIndex:11];
        tempDataContenet=[tempDataContenet stringByAppendingString:@" "];
        //组合正文标题＋时间
        NSMutableString *tempData=[[NSMutableString alloc]init];
        [tempData insertString:tempDataTime atIndex:0];
        [tempData insertString:tempDataContenet atIndex:0];
        [_titleArray addObject:tempData];  
        
    }
    self.titleArray=_titleArray;
    self.linkArray=_linkArray;
}

@end


















